package net.kuaidi100.demo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by Kong on 16/12/28.
 */

public class searchKuaiDi extends HttpServlet  {


    private static final long serialVersionUID = 1L;

    public searchKuaiDi() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2017-02-08&leftTicketDTO.from_station=SEQ&leftTicketDTO.to_station=VUQ&purpose_codes=ADULT
        String strHost = "http://www.kuaidi100.com/query";
        String strPsType = request.getParameter("psType");
        String strNumber = request.getParameter("number");
        String html = "";
        String message = null;
        try {
            if(null == strPsType || 0 == strPsType.length()){
                message = "参数不能为空";
                html = "{\"errorCode\":\"N\",\"errorMessage\":\""+ message +"\",\"result\":null,\"succeeded\":false}";
            }else if(null == strNumber || 0 == strNumber.length()){
                message = "请求路径不能为空";
                html = "{\"errorCode\":\"N\",\"errorMessage\":\""+ message +"\",\"result\":null,\"succeeded\":false}";
            }else{
                html = HttpRequest.sendGet(strHost,"type="+strPsType+"&postid="+strNumber);
            }
        } catch (Exception e) {
            html = "{\"errorCode\":\"N\",\"errorMessage\":\""+ e.getMessage() +"\",\"result\":null,\"succeeded\":false}";
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(html);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
}
